package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.dto.SimDTO;
import com.tbit.uqbike.object.pojo.vo.SimVo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.SimManageService;
import com.tbit.uqbike.webmanager.dao.core.SimManagerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * sim卡管理
 *
 * @Author LHX
 * @ClassName SimManageServiceImpl
 * @Description
 * @date 2023/5/5 11:59
 * @Version 1.0
 */
@Service
public class SimManageServiceImpl implements SimManageService {

    @Autowired
    private SimManagerDao simManagerDao;


    @Override
    public PageResult<SimVo> queryPage(SimDTO simDTO) {
        if (StrUtil.isNotEmpty(simDTO.getAccountIds())){
            List<Integer> accountIdList = StrUtil.split(simDTO.getAccountIds(), ",").stream()
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            simDTO.setAccountIdList(accountIdList);
        }
        PageInfo<SimVo> pageInfo = PageHelper.startPage(simDTO.getPage(), simDTO.getSize()).doSelectPageInfo(() -> simManagerDao.queryList(simDTO));
        return new PageResult<>(simDTO.getPage(), simDTO.getSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public List<SimVo> export(SimDTO simDTO) {
        if (StrUtil.isNotEmpty(simDTO.getAccountIds())){
            List<Integer> accountIdList = StrUtil.split(simDTO.getAccountIds(), ",").stream()
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            simDTO.setAccountIdList(accountIdList);
        }
        PageInfo<SimVo> pageInfo = PageHelper.startPage(1, 65535).doSelectPageInfo(() -> simManagerDao.queryList(simDTO));
        int size = 65535;
        if (pageInfo.getTotal() > size) {
            throw new BaseException("导出数据过大，请调整查询范围");
        }
        return pageInfo.getList();
    }

}
